using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IWS.Lib.Utilities.Web;

public partial class Admin_Controls_UserForm : IWS.Lib.Utilities.Web.FormControl
{
	protected void Page_Load(object sender, EventArgs e)
	{
		MessageBox1.Visible = false;

		if (!Page.IsPostBack)
		{
			this.Initialize();
		}
	}

	protected override void InitializeCustom()
	{
		this.ResetForm();
		lblNew.Text = "true";
		lblID.Text = string.Empty;
	}

	protected override void OnSaveClick(object sender, EventArgs e)
	{
		if (Page.IsValid)
		{
			try
			{
				bool isNew = Convert.ToBoolean(lblNew.Text);
				MembershipUser user;
				if (isNew)
				{
					MembershipCreateStatus status;
					user = System.Web.Security.Membership.Provider.CreateUser(txtUserID.Text, txtPass1.Text, txtEmail.Text, txtQuestion.Text,txtAnswer.Text, true, Guid.NewGuid(), out status);
					if (status != MembershipCreateStatus.Success)
					{
						this.MessageBox1.ShowMessage("Cannot create user. <br/>Error: " + status.ToString(), IWS.Lib.Utilities.Web.MessageBox.Type.Error);
					}
					else
					{
						lblID.Text = user.UserName;
						this.MessageBox1.ShowMessage("Updated successfully.", IWS.Lib.Utilities.Web.MessageBox.Type.Normal);
						base.OnSaveClick(sender, e);
						base.OnAfterSave(sender, new SaveEventArgs(user.UserName));
					}
				}
				else
				{
					user = System.Web.Security.Membership.Provider.GetUser(lblID.Text, false);
					if (user != null)
					{
						this.UpdateUser(ref user);
						this.MessageBox1.ShowMessage("Updated successfully.", IWS.Lib.Utilities.Web.MessageBox.Type.Normal);

						base.OnSaveClick(sender, e);
						base.OnAfterSave(sender, new SaveEventArgs(user.UserName));
					}
				}

			}
			catch (Exception ex)
			{
				this.LogException(ex, System.Reflection.Assembly.GetExecutingAssembly());

				this.MessageBox1.MessageType = IWS.Lib.Utilities.Web.MessageBox.Type.Error;
				this.MessageBox1.ShowMessage(ex.Message);
			}
		}
	}
	protected override void OnDeleteClick(object sender, EventArgs e)
	{
		try
		{
			if (System.Web.Security.Membership.Provider.DeleteUser(lblID.Text, true))
			{
				this.MessageBox1.ShowMessage("Successfully deleted user.", IWS.Lib.Utilities.Web.MessageBox.Type.Normal);
				this.ResetForm();

				base.OnDeleteClick(sender, e); //finish up. 
			}
		}
		catch (Exception ex)
		{
			this.LogException(ex, System.Reflection.Assembly.GetExecutingAssembly());

			this.MessageBox1.ShowMessage(ex.Message, IWS.Lib.Utilities.Web.MessageBox.Type.Error);
		}
	}

	public void LoadUser(string username)
	{
		this.Initialize();
		bool isNew = true;

		if (username != null && username.Length > 0)
		{
			this.lblTitle.Text = username;
			isNew = false;
		}

		lblNew.Text = isNew.ToString();
		txtPass1.Enabled = isNew;
		txtPass2.Enabled = isNew;
		txtQuestion.Enabled = isNew;
		txtAnswer.Enabled = isNew;
		cmdDelete.Visible = !isNew;
		txtComments.Enabled = !isNew;

		if (!isNew)
		{
			lblID.Text = username;
			MembershipUser user = System.Web.Security.Membership.Provider.GetUser(lblID.Text, false);
			if (user != null)
				this.SetForm(user);
			else
			{
				lblID.Text = string.Empty;
				this.ResetForm();
			}
		}
	}
	private void UpdateUser(ref MembershipUser user)
	{
		bool isNew = Convert.ToBoolean(lblNew.Text);
		if (isNew)
		{
			MembershipCreateStatus status;
			System.Web.Security.Membership.Provider.CreateUser(txtUserID.Text, txtPass1.Text, txtEmail.Text, txtQuestion.Text, txtAnswer.Text, chkApproved.Checked, null, out status);
			if (status != MembershipCreateStatus.Success)
			{
				this.MessageBox1.ShowMessage("Could not create user. Error: " + status.ToString(), IWS.Lib.Utilities.Web.MessageBox.Type.Error);
			}
		}
		else
		{
			user.Comment = txtComments.Text;
			user.Email = txtEmail.Text;
			user.IsApproved = chkApproved.Checked;

			System.Web.Security.Membership.Provider.UpdateUser(user);
		}
	}
	private void SetForm(MembershipUser user)
	{
		this.lblID.Text = user.UserName;
		this.txtUserID.Text = user.UserName;
		this.txtUserID.Enabled = false;
		this.txtEmail.Text = user.Email;
		this.txtUserID.Text = user.UserName;
		this.chkApproved.Checked = user.IsApproved;
		this.txtComments.Text = user.Comment;
		this.txtQuestion.Text = user.PasswordQuestion;
	}
	private void ResetForm()
	{
		this.txtEmail.Text = string.Empty;
		this.txtUserID.Text = string.Empty;
		this.txtPass1.Text = string.Empty;
		this.txtPass2.Text = string.Empty;
		this.txtQuestion.Text = string.Empty;
		this.txtAnswer.Text = string.Empty;
		this.chkApproved.Checked = false;
		this.txtComments.Text = string.Empty;
	}
}
